﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers.WebApi
{
    public class SendCarApiController : ApiController
    {
        /// <summary>
        /// 获取未派车的申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetPageData()
        {
            int orgz = Convert.ToInt32(HttpContext.Current.Request["orgzNo"]);
            var tmp = new Car_UseCarApplicationService().GetCarUseList(orgz);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取未派出车辆
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetNoSendCar()
        {
            int orgz = Convert.ToInt32(HttpContext.Current.Request["orgzNo"]);
            var tmp = new CarInfoService().GetList(orgz, 1, string.Empty,0);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取司机
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetDriver()
        {
            int orgz = Convert.ToInt32(HttpContext.Current.Request["orgzNo"]);
            var tmp = new CarInfoService().GetUser(orgz, string.Empty);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 保存派车数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object SaveData()
        {
            string data = HttpContext.Current.Request["data"];
            JavaScriptSerializer json = new JavaScriptSerializer();
            IList<SendCarRecord> list = json.Deserialize<IList<SendCarRecord>>(data);
            var flag = new SendCarRecordService().SaveData(list);
            return flag;
        }
        /// <summary>
        /// 拒绝派车
        /// </summary>
        /// <param name="id">用车申请ID</param>
        /// <param name="context">拒绝内容</param>
        /// <param name="userId">拒绝人</param>
        /// <returns></returns>
        [HttpPost]
        public object RefuseSendCar()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            string context = HttpContext.Current.Request["context"];
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            return new Car_UseCarApplicationService().RefuseSendCar(id, context, userId);
        }

        /// <summary>
        /// 消息数量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public int GetMsgCount()
        {
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int orgz = Convert.ToInt32(HttpContext.Current.Request["orgzNo"]);
            int carusecount = new Car_UseCarApplicationService().GetCarUseList(userId, orgz).Count;
            return carusecount;
        }
    }
}

